<?php
declare (strict_types=1);

namespace app\api\controller\v3;

use app\common\model\Datum;
use app\common\model\DatumCate;
use app\common\model\Record;
use app\common\model\Unit as UnitModel;
use function Couchbase\defaultDecoder;

class Unit extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */

    public function index()
    {
        $id = input('id/d');
        $where['status'] = 1;
        $where['datum_cate_id'] = $id;
        $unit = Datum::where($where)
                ->field('id,unit_id,datum_cate_id')
                ->group('unit_id')
                ->order('unit_id','asc')
                ->select();
        foreach ($unit as $k => $v) {
            $where['unit_id'] = $v['unit_id'];
            $a['unit_id'] = $v['unit_id'];
            $a['datum_cate_id'] = $v['datum_cate_id'];
            $unit[$k]['unit_total'] = Datum::where($where)->count();
            $unit[$k]['unit_work_total'] = Record::where($a)->where(['api_member_id' => $this->user_id])->count();
            $v['unit_id'] = $v->unit->title;
            //销毁变量
            unset($v->unit);
        }
        return sucessMsg(200, '获取成功', ['unit' => $unit]);
    }
}
